package com.aiatomic.couting.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.aiatomic.couting.IConstant;
import com.aiatomic.couting.account.CurrencyChangeEventDispatcher;
import com.aiatomic.couting.models.GoodEntity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class AppUtils {
	public static int sid;
	public static String aid;
	public static String sname;
	public static String url;
	public static String sUrl;
	public static String nick;
	public static String sKey;
	public static String mob;
	public static int channel;
	public static int currency_day;
	public static int order_clean;
	public static List<GoodEntity> goods = new ArrayList<GoodEntity>();

	public static void clean() {
		sid = 0;
		aid = null;
		sname = null;
		url = null;
		sUrl = null;
		nick = null;
		sKey = null;
		channel = 0;
		goods.clear();
	}

	public static boolean isLogin(Context context) {
		if (aid != null && !aid.isEmpty()) {
			return true;
		}
		return false;
	}
	private static Handler handler=new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == HttpUtils.CODE_SUCCESS_RQ_100) {
				try {
					JSONObject json = new JSONObject(msg.obj.toString());
					if (json.getInt("code") == HttpUtils.RET_SUCCESS) {
						CurrencyChangeEventDispatcher.getInstance().dispatchEvent(CurrencyChangeEventDispatcher.EVENT_CODE_DAYS);
						AppUtils.currency_day = json.getJSONObject("currency").getInt("days");
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
	};
	public static void refresh_currency(final Context context) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("platform", String.valueOf(IConstant.PLATFORM));
		params.put("aid", AppUtils.aid);
		params.put("client_version", SystemUtil.getAppVersion(context));
		params.put("time", String.valueOf(Calendar.getInstance().getTimeInMillis()));
		params.put("channel", String.valueOf(AppUtils.channel));
		params.put("sign", HttpUtils.md5(params.get("aid") + params.get("time") + params.get("client_version")
				+ params.get("platform") + params.get("channel") + IConstant.NORMAL_MD5_KEY));
		params.put("ssign", HttpUtils.md5(params.get("aid") + params.get("time") + params.get("client_version")
				+ params.get("platform") + params.get("channel") + AppUtils.sKey));
		HttpUtils.post(handler, AppUtils.url + "/v/cindex/refresh_currency/", params);
	}
}
